package com.huike.web.controller.business;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.huike.common.core.domain.entity.SysDictData;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.service.ITbBusinessService;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;

/**
 * 商机跟进记录Controller
 *
 * @date 2021-04-28
 */
@RestController
@RequestMapping("/business/record")
public class TbBusinessTrackRecordController extends BaseController {

    @Autowired
    private ITbBusinessTrackRecordService tbBusinessTrackRecordService;

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private ITbBusinessService businessService;

    /**
     * 查询商机跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('business:record:list')")
    @GetMapping("/list")
    public AjaxResult list(@RequestParam("businessId") Long id) {
        List<TbBusinessTrackRecord> tbBusinessTrackRecordList = businessService.list(id);
        for (TbBusinessTrackRecord tbBusinessTrackRecord : tbBusinessTrackRecordList) {
            String keyItems = tbBusinessTrackRecord.getKeyItems();
            List<String> keys = new ArrayList<>();
            String[] split = keyItems.replace("\"", "").trim().split(",");
            for (String s : split) {
                String communication_point = sysDictDataService.selectDictLabel("communication_point", s);
                keys.add(communication_point);
            }
            tbBusinessTrackRecord.setKeys(keys);
        }
        return AjaxResult.success(tbBusinessTrackRecordList);
    }

    /**
     * 新增商机跟进记录
     */
    @PreAuthorize("@ss.hasPermi('business:record:add')")
    @Log(title = "商机跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessTrackVo businessTrackVo) {
        //构建商机对象和商机跟进记录对象
        TbBusiness business = new TbBusiness();

        TbBusinessTrackRecord businessTrackRecord = new TbBusinessTrackRecord();

        //将接收来的参数复制到新构建的对象中
        BeanUtils.copyProperties(businessTrackVo, business);
        BeanUtils.copyProperties(businessTrackVo, businessTrackRecord);

        //设置商机跟进记录对象的创建时间和创建人
        //businessTrackRecord.setBusinessId(businessTrackVo.getBusinessId()); //加不加都可以,因为TbBusinessTrackRecord实体中已经有了Business属性,上面的copyProperties方法可以赋值进去
        businessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        businessTrackRecord.setCreateTime(new Date());

        //设置商机状态为跟进中
        business.setStatus("2");
        business.setId(businessTrackVo.getBusinessId());

        //调用service
        businessService.updateBusiness(business);
        tbBusinessTrackRecordService.save(businessTrackRecord);
        return AjaxResult.success();
    }
}